package com.loli;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.json.Json;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.constants.Constants;
import com.model.HttpResponseObject;
import com.utils.HttpUtils;

public class ImgListActivity extends Activity {

	private ListView imgListView;
	HandlerThread handlerThread;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.img_list);

		imgListView = (ListView) findViewById(R.id.imgListView);

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pageNo", 1);
		params.put("pageSize", 6);
		HttpResponseObject hro = HttpUtils
				.getInfoFromUrl(Constants.USER_LIST_URL + "pageNo=1");
		@SuppressWarnings("unchecked")
		List<Map<String, Object>> mapList = (List<Map<String, Object>>) Json
				.fromJson(hro.getResContent());
		ImgSimpleAdapter adapter = new ImgSimpleAdapter(this, mapList,
				R.layout.img_list_view, new String[] { "imgs", "title",
						"infoId" }, new int[] { R.id.imgs, R.id.title,
						R.id.infoId });
		imgListView.setAdapter(adapter);

	}

	class ImgSimpleAdapter extends SimpleAdapter {

		public ImgSimpleAdapter(Context context,
				List<? extends Map<String, ?>> data, int resource,
				String[] from, int[] to) {
			super(context, data, resource, from, to);
		}

		@Override
		public void setViewImage(ImageView v, String value) {
			v.setOnClickListener(new ImgDetailOnClickListener());
			Handler handler = new Handler();
			BitmapThread r = new BitmapThread(value, v);
			handler.post(r);
		}

		@Override
		public void setViewText(TextView v, String text) {
			if (text.length() > 40) {
				text = text.substring(0, 40) + "……";
			}
			v.setOnClickListener(new ImgDetailOnClickListener());
			super.setViewText(v, text);
		}

	}

	class BitmapThread implements Runnable {

		private String url;
		private ImageView iv;

		public BitmapThread() {

		}

		public BitmapThread(String url, ImageView iv) {
			this.url = url;
			this.iv = iv;
		}

		@Override
		public void run() {
			iv.setImageBitmap(HttpUtils.returnBitMap(url));
		}

	}

	class ImgDetailOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			LinearLayout ll = (LinearLayout) v.getParent();
			TextView textView = (TextView) ll.getChildAt(2);
			String text = textView.getText().toString();
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("infoId", text);
			intent.putExtras(bundle);
			intent.setClass(ImgListActivity.this, ImgDetailActivity.class);
			startActivity(intent);
		}

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

}
